package com.base.load

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.animation.AnimationUtils
import android.view.animation.LinearInterpolator
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.base.R

/**
 * 默认的LoadView
 */
class DefaultLoad : LinearLayout, LoadControl {

    private lateinit var loadView: ImageView
    private lateinit var titleView: TextView
    private lateinit var msgView: TextView

    private var state = LoadState.NONE

    constructor(context: Context) : super(context) {
        initView(context)
    }

    private fun initView(context: Context) {
        orientation = VERTICAL
        loadView = ImageView(context)
        titleView = TextView(context)
        msgView = TextView(context)

        val params = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        params.gravity = Gravity.CENTER
        params.bottomMargin = 10
        addView(titleView, params)

        val params1 = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        params1.gravity = Gravity.CENTER
        addView(loadView, params1)

        val params2 = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        params2.gravity = Gravity.CENTER
        addView(msgView, params2)
    }

    override fun load(view: ViewGroup) {
        addView(view)
        state = LoadState.LOAD
        loadView.visibility = View.VISIBLE
        loadView.setImageResource(R.mipmap.load)
        loadView.clearAnimation()
        val operatingAnim = AnimationUtils.loadAnimation(context, R.anim.rotate_anim)
        val lin = LinearInterpolator()
        operatingAnim.interpolator = lin
        loadView.startAnimation(operatingAnim)
        titleView.visibility = View.GONE
        msgView.visibility = View.GONE
    }

    override fun error(view: ViewGroup) {
        addView(view)
        state = LoadState.ERROR
        loadView.visibility = View.GONE
        loadView.clearAnimation()
        titleView.visibility = View.VISIBLE
        msgView.visibility = View.VISIBLE
    }

    override fun net(view: ViewGroup) {
        addView(view)
        state = LoadState.NET
        loadView.visibility = View.GONE
        loadView.clearAnimation()
        titleView.visibility = View.VISIBLE
        msgView.visibility = View.VISIBLE
    }

    override fun notice(view: ViewGroup) {
        addView(view)
        state = LoadState.NOTICE
        loadView.visibility = View.GONE
        loadView.clearAnimation()
        titleView.visibility = View.VISIBLE
        msgView.visibility = View.VISIBLE
    }

    override fun addView(view: ViewGroup) {
        removeView()
        if (view is FrameLayout) {
            val params = FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER)
            params.gravity = Gravity.CENTER
            view.addView(this, view.childCount, params)
        } else {
            view.addView(this, view.childCount)
        }
    }

    override fun removeView() {
        if (parent != null) {
            (parent as ViewGroup).removeView(this)
        }
    }

    override fun getState(): LoadState {
        return state
    }
}